package com.example.demo.practice.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by hanbo on 2018-01-05.
 */

public class GradientTextView extends TextView {
    public GradientTextView(Context context) {
        super(context);
    }

    public GradientTextView(Context context,
                            AttributeSet attrs) {
        super(context, attrs);
    }

    public GradientTextView(Context context,
                            AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed,
                            int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            getPaint().setShader(new LinearGradient(
                    0, 0, getWidth(), 0,
                    StringToColor("#4ef1ff"), StringToColor("#0e9aeb"),
                    Shader.TileMode.CLAMP));
        }
    }

    /**
     * #颜色转16进制颜色
     * @param str {String} 颜色
     * @return
     */
    private  int StringToColor(String str) {
        return Color.parseColor(str);
    }
}
